package ar.com.notarip.bb.infosubte.util;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;

public class ImageCheckboxField extends BaseButtonField {

	   private boolean _checked = false;
	   private Bitmap[] _bitmaps;
	   private static final int UNCHECKED = 0;
	   private static final int CHECKED = 1;
	   private static final int FOCUS_UNCHECKED = 2;
	   private static final int FOCUS_CHECKED = 3;

	   public ImageCheckboxField( boolean checked, String imgChecked, String imgUnchecked, String imgCheckedFocus, String imgUncheckedFocus )
	   {
		   this( checked, imgChecked, imgUnchecked, imgCheckedFocus, imgUncheckedFocus,  0 );
		  setChecked(checked);

	   }
	   

	   protected ImageCheckboxField( boolean checked,  String checkedState, String uncheckedState, 
			   String checkedStateFocus, String uncheckedStateFocus,   long style ) {
		   
		   
	      super( Field.FOCUSABLE | style );
		  
	      
	      int height = 40;
	      int width = 40;

	      /*Se hacen los resize la de las imagenes */

		   Bitmap bmpUnchk = Util.getBitmapResized(uncheckedState, height, width);
		   Bitmap bmpChk = Util.getBitmapResized(checkedState, height, width);
		   
		   Bitmap bmpUnchkFocus = Util.getBitmapResized(uncheckedStateFocus, height, width);
		   Bitmap bmpChkFocus = Util.getBitmapResized(checkedStateFocus, height, width);

		   
		   
	      if( (bmpUnchk.getWidth() != bmpChk.getWidth())
	            || (bmpUnchk.getHeight() != bmpChk.getHeight()) ){

	         throw new IllegalArgumentException( "Image sizes don't match" );
	      }

	      _bitmaps = new Bitmap[] { bmpUnchk, bmpChk, bmpUnchkFocus, bmpChkFocus };
	   }

	   public void clickButton() {
	      // override this to toggle state on click
		  
	      setChecked(!getChecked());
	      super.clickButton();
	   }

	   public void setChecked(boolean checked) {
	      _checked = checked;
	      invalidate();
	   }

	   public boolean getChecked() {
	      return _checked;
	   }        

	   private void setImage( Bitmap uncheckedState ){
	      _bitmaps[UNCHECKED] = uncheckedState;
	      invalidate();
	   }

	   private void setCheckedImage( Bitmap checkedState ){
	      _bitmaps[CHECKED] = checkedState;
	      invalidate();
	   }

	   public int getPreferredWidth() {
	      return _bitmaps[UNCHECKED].getWidth();
	   }

	   public int getPreferredHeight() {
	      return _bitmaps[UNCHECKED].getHeight();
	   }

	   protected void layout( int width, int height ) {
	      setExtent( _bitmaps[UNCHECKED].getWidth(), _bitmaps[UNCHECKED].getHeight() );
	   }

	   protected void paint( Graphics g ) {
	      // TODO: uncomment this if you draw different images based on focus
	      boolean focused = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
	      int index = UNCHECKED;
	      if (focused) {
	         index = getChecked() ? FOCUS_CHECKED : FOCUS_UNCHECKED;
	      } else {
	    	  index = getChecked() ? CHECKED : UNCHECKED;
	      }      
	      g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
	   }

	   /**
	    * With this commented out the default focus will show through
	    * If an app doesn't want focus colours then it should override this and do nothing
	    **/
	   /*
	       protected void paintBackground( Graphics g ) {
	           // Nothing to do here
	       }
	    */

	   protected void drawFocus( Graphics g, boolean on ) {
	      // Paint() handles it all
	      g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
	      paintBackground( g );
	      paint( g );
	   }


	}